package com.tgh.gles2.c2.triangle;

import com.tgh.utils.GLShaderManager;
import com.tgh.utils.GLTools;
import com.tgh.utils.GLShaderManager.ShaderType;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.util.AttributeSet;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MySurfaceView extends GLSurfaceView {

    public static final String TAG = "MySurfaceView";

    public MySurfaceView(Context context) {
        super(context);
        init();
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    private void init() {
        setEGLContextClientVersion(2);
        setEGLConfigChooser(8, 8, 8, 8, 16, 1);
        setRenderer(new SceneRenderer());
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
    
    private class SceneRenderer implements Renderer{
        
        private GLShaderManager shaderManager;
        private FloatBuffer verticesBuffer;
        private float[] red=new float[]{1,0,0,1};
        private float[] black=new float[]{0,0,0,1};
        
        private float[] worldMatrix=new float[16];
        private float[] projectMatrix=new float[16];
        private float[] mvpMatrix=new float[16];
        
        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            shaderManager=new GLShaderManager();
            shaderManager.initStockShader();
            float[] vertices=new float[]{
                    -0.25f,  0f, 0f,   
                    0.25f, 0f, 0f,
                    0f, 0.5f, 0.0f
            };
            verticesBuffer = GLTools.makeFloatBuffer(vertices);
            GLES20.glClearColor(0f, 0f, 1f, 1f);
            GLES20.glEnable(GLES20.GL_DEPTH_TEST);
        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            GLES20.glViewport(0, 0, width, height);
            float ratio=width/height;
            Matrix.orthoM(projectMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
        }

        @Override
        public void onDrawFrame(GL10 gl) {
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
            
            Matrix.setLookAtM(worldMatrix, 0, 0, 0, -3, 0, 0, 0, 0, 1, 0);
            Matrix.multiplyMM(mvpMatrix, 0, projectMatrix, 0, worldMatrix, 0);
            
            shaderManager.useStockShader(ShaderType.FLAT,mvpMatrix,red);
            
            
            GLES20.glVertexAttribPointer(GLShaderManager.GL_ATTRIBUTE_VERTEX, 3, GLES20.GL_FLOAT, false, 0, verticesBuffer);
            GLES20.glEnableVertexAttribArray(GLShaderManager.GL_ATTRIBUTE_VERTEX);
            
            GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
            
           
            GLES20.glEnable(GLES20.GL_POLYGON_OFFSET_FILL);
            GLES20.glPolygonOffset(-1, -1);
            
            GLES20.glEnable(GLES20.GL_BLEND);
            GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
            
            GLES20.glLineWidth(5f);
            
            shaderManager.useStockShader(ShaderType.FLAT, mvpMatrix,black);
            
            GLES20.glDrawArrays(GLES20.GL_LINE_LOOP, 0, 3);
            
            GLES20.glDisable(GLES20.GL_POLYGON_OFFSET_FILL);
            GLES20.glDisable(GLES20.GL_BLEND);
            GLES20.glLineWidth(1.0f);
        }
       
    }
}
